package com.jiulan.hearthearing.ui.union.index;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseVPFragment;
import com.jiulan.hearthearing.ui.menus.MenuAdapter;
import com.jiulan.hearthearing.ui.menus.MenuModel;
import com.stx.xhb.xbanner.XBanner;

import java.util.ArrayList;
import java.util.List;

/**
 * 国医联盟
 */
public class UnionIndex extends BaseVPFragment implements View.OnClickListener {
    private Context context;
    private MenuAdapter menuAdapter;
    private List<MenuModel> menuModels;

    private View view;
    private XBanner banner;
    private RecyclerView rcvActive;

    public static UnionIndex newInstance(String content) {
        Bundle arguments = new Bundle();
        arguments.putString("string", content);
        UnionIndex fl = new UnionIndex();
        fl.setArguments(arguments);
        return fl;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_union_index, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }

    private void initView() {
        banner = view.findViewById(R.id.banner);
        rcvActive = view.findViewById(R.id.rcvActive);
        rcvActive.setLayoutManager(new GridLayoutManager(context, 4));
        setTools();
        setBanner();
    }

    /**
     * banner
     */
    private void setBanner() {
//        String result = "{\"status\":0,\"msg\":\"\\u83b7\\u53d6\\u5e7b\\u706f\\u7247\\u5217\\u8868\\u6210\\u529f\",\"data\":[{\"id\":\"1\",\"title\":\"\\u8f6e\\u64ad\\u4e00\",\"image\":\"http:\\/\\/t.9lan.cn\\/attachment\\/images\\/4\\/2020\\/03\\/sZNiQpFUnNzBzngoqIIx4Q2NinrrAu.jpg\",\"type\":\"0\",\"url\":\"www.baidu.com\"},{\"id\":\"2\",\"title\":\"\\u8f6e\\u64ad\\u4e8c\",\"image\":\"http:\\/\\/t.9lan.cn\\/attachment\\/images\\/4\\/2020\\/03\\/KgTgoozU4uDgKm5l0z3tH40mhdTlU7.jpg\",\"type\":\"0\",\"url\":\"www.baidu.com\"}]}";
//        try {
//            JSONObject object = new JSONObject(result);
//            if (object.has("status")) {
//                if (object.getInt("status") == 0) {
//                    banner.setVisibility(View.VISIBLE);
//                    GsonBuilder builder = new GsonBuilder();
//                    Gson gson = builder.create();
//                    final BannerModel bannerModel = gson.fromJson(result, BannerModel.class);
//                    banner.setBannerData(bannerModel.getData());
//                    //设置广告图片点击事件
//                    banner.setOnItemClickListener(new XBanner.OnItemClickListener() {
//                        @Override
//                        public void onItemClick(XBanner banner, Object model, View view, int position) {
//                            Toast.makeText(context, "点击了第" + (position + 1) + "图片", Toast.LENGTH_SHORT).show();
//                        }
//                    });
//                    //加载广告图片
//                    banner.loadImage(new XBanner.XBannerAdapter() {
//                        @Override
//                        public void loadBanner(XBanner banner, Object model, View view, int position) {
//                            //在此处使用图片加载框架加载图片，demo中使用glide加载，可替换成自己项目中的图片加载框架
//                            Glide.with(context).load(bannerModel.getData().get(position).getXBannerUrl()).placeholder(R.mipmap.union_banner).error(R.mipmap.union_banner).into((ImageView) view);
//                        }
//                    });
//                } else {
//                    banner.setVisibility(View.GONE);
//                    Toast.makeText(context, "获取广告数据失败", Toast.LENGTH_SHORT).show();
//                }
//            }
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
    }

    private void setTools() {
        menuModels = new ArrayList<>();
        menuModels.add(new MenuModel(R.mipmap.union_xianshangzixun, "线上咨询"));
        menuModels.add(new MenuModel(R.mipmap.union_menzhenguahao, "门诊挂号"));
        menuModels.add(new MenuModel(R.mipmap.union_wangdianchaxun, "网点查询"));
        menuModels.add(new MenuModel(R.mipmap.union_luxiandaohang, "路线导航"));
        menuAdapter = new MenuAdapter(menuModels);
        rcvActive.setAdapter(menuAdapter);
        menuAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (menuModels.get(position).getActiveName()) {
                    case "线上咨询":
                        Toast.makeText(context, "线上咨询", Toast.LENGTH_SHORT).show();
                        break;
                    case "门诊挂号":
                        Toast.makeText(context, "门诊挂号", Toast.LENGTH_SHORT).show();
                        break;
                    case "网点查询":
                        Toast.makeText(context, "网点查询", Toast.LENGTH_SHORT).show();
                        break;
                    case "路线导航":
                        Toast.makeText(context, "路线导航", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {

    }

    @Override
    protected void onFragmentFirstVisible() {

    }

    @Override
    public void onClick(View v) {

    }
}
